﻿//============================================================
//author:zhxitao
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Saker.Kernel.Model;

namespace Saker.Kernel.DAL
{
    #region SysRoleAuthorityList数据访问层
	/// <summary>
	///SysRoleAuthorityList数据访问层
	/// </summary>	
	public partial class SysRoleAuthorityListDAL
	{
        #region SysRoleAuthorityList数据访问层 自动生成
		/// <summary>
        /// 添加SysRoleAuthorityList
        /// </summary>
        /// <param name="SysRoleAuthorityList">SysRoleAuthorityList</param>
        /// <returns>添加后的SysRoleAuthorityList</returns>
        public SysRoleAuthorityList Add
			(SysRoleAuthorityList sysRoleAuthorityList)
		{
				string sql ="INSERT INTO SysRoleAuthorityList (UserID, RoleID, ModuleID, AuthorityTag, Flag)  VALUES (@UserID, @RoleID, @ModuleID, @AuthorityTag, @Flag) select @@identity";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@UserID", ToDBValue(sysRoleAuthorityList.UserID)),
						new SqlParameter("@RoleID", ToDBValue(sysRoleAuthorityList.RoleID)),
						new SqlParameter("@ModuleID", ToDBValue(sysRoleAuthorityList.ModuleID)),
						new SqlParameter("@AuthorityTag", ToDBValue(sysRoleAuthorityList.AuthorityTag)),
						new SqlParameter("@Flag", ToDBValue(sysRoleAuthorityList.Flag)),
					};
					
				int newId = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, para));
				return GetByID(newId);
		}

		/// <summary>
        /// 根据主键删除SysRoleAuthorityList
        /// </summary>
        /// <param name="ID">ID</param>
        /// <returns>影响的行数</returns>
        public int DeleteByID(int iD)
		{
            string sql = "DELETE SysRoleAuthorityList WHERE ID = @ID";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
		/// <summary>
		/// 更新SysRoleAuthorityList
		/// </summary>
		/// <param name="SysRoleAuthorityList">SysRoleAuthorityList</param>
		/// <returns></returns>
        public int Update(SysRoleAuthorityList sysRoleAuthorityList)
        {
            string sql =
                "UPDATE SysRoleAuthorityList " +
                "SET " +
			" UserID = @UserID" 
                +", RoleID = @RoleID" 
                +", ModuleID = @ModuleID" 
                +", AuthorityTag = @AuthorityTag" 
                +", Flag = @Flag" 
               
            +" WHERE ID = @ID";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", sysRoleAuthorityList.ID)
					,new SqlParameter("@UserID", ToDBValue(sysRoleAuthorityList.UserID))
					,new SqlParameter("@RoleID", ToDBValue(sysRoleAuthorityList.RoleID))
					,new SqlParameter("@ModuleID", ToDBValue(sysRoleAuthorityList.ModuleID))
					,new SqlParameter("@AuthorityTag", ToDBValue(sysRoleAuthorityList.AuthorityTag))
					,new SqlParameter("@Flag", ToDBValue(sysRoleAuthorityList.Flag))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
		/// <summary>
		/// 根据主键查询SysRoleAuthorityList实体
		/// </summary>
		/// <param name="SysRoleAuthorityList">SysRoleAuthorityList</param>
		/// <returns>SysRoleAuthorityList</returns>
        public SysRoleAuthorityList GetByID(int iD)
        {
            string sql = "SELECT * FROM SysRoleAuthorityList WHERE ID = @ID";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@ID", iD)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		/// <summary>
		/// SqlDataReader对象转换为SysRoleAuthorityList实体
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysRoleAuthorityList实体</returns>
		public SysRoleAuthorityList ToModel(SqlDataReader reader)
		{
			SysRoleAuthorityList sysRoleAuthorityList = new SysRoleAuthorityList();

			sysRoleAuthorityList.ID = (int)ToModelValue(reader,"ID");
			sysRoleAuthorityList.UserID = (int)ToModelValue(reader,"UserID");
			sysRoleAuthorityList.RoleID = (int)ToModelValue(reader,"RoleID");
			sysRoleAuthorityList.ModuleID = (int)ToModelValue(reader,"ModuleID");
			sysRoleAuthorityList.AuthorityTag = (string)ToModelValue(reader,"AuthorityTag");
			sysRoleAuthorityList.Flag = (bool)ToModelValue(reader,"Flag");
			return sysRoleAuthorityList;
		}
		/// <summary>
		/// 得到数据总行数
		/// </summary>
		/// <returns></returns>
		public int GetTotalCount()
		{
			string sql = "SELECT count(1) FROM SysRoleAuthorityList";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		/// <summary>
		/// 分页，此方法仅在sql 2005下使用
		/// </summary>
		/// <param name="minrownum">起始行数</param>
		/// <param name="maxrownum">>结束行数</param>
		/// <returns>SysRoleAuthorityList集合</returns>
		public IEnumerable<SysRoleAuthorityList> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by ID) rownum FROM SysRoleAuthorityList) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
        /// <summary>
        /// 分页，调用存储过程
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="whereCondition">where</param>
        /// <returns>DataTable数据集</returns>
        public DataTable GetPagingRecord(int pageIndex, ref int recordCount, int pageSize, ref int pageCount,string whereCondition)
        {
            return SqlHelper.GetPagingRecord(pageIndex,ref recordCount,pageSize,ref pageCount,"SysRoleAuthorityList","ID",whereCondition,"*","ID",true);
        }
		/// <summary>
		/// 得到所有SysRoleAuthorityList集合
		/// </summary>
		/// <returns>所有集合</returns>
		public IEnumerable<SysRoleAuthorityList> GetAll()
		{
			string sql = "SELECT * FROM SysRoleAuthorityList";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		/// <summary>
		/// SqlDataReader对象转换到SysRoleAuthorityList集合
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysRoleAuthorityList集合</returns>
		protected IEnumerable<SysRoleAuthorityList> ToModels(SqlDataReader reader)
		{
			var list = new List<SysRoleAuthorityList>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		/// <summary>
		/// 转换数据值
		/// </summary>
		/// <param name="value">object类型</param>
		/// <returns>object对象</returns>
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		/// <summary>
		/// 转换实体值
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <param name="columnName">列名称</param>
		/// <returns>值</returns>
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
        #endregion
	}
    #endregion
}
